package com.example.administrator.wallet.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.wallet.R;
import com.example.administrator.wallet.model.bean.RecordBean;
import com.example.administrator.wallet.view.activity.asset.RecordActivity;

import java.util.List;

import static android.view.View.VISIBLE;

/**
 * Effect:
 * <p>
 * Author:  CaiYapeng
 * <p>
 * Time  :  2018/7/12.
 */

public class RecordListAdapter extends BaseAdapter {
    private Context context;
    private List<RecordBean.DataBean> iDatas;
    private int value = 1;
    private OnClick click;

    public RecordListAdapter(RecordActivity recordActivity, List<RecordBean.DataBean> iDatas) {
        this.context = recordActivity;
        this.iDatas=iDatas;
    }

    @Override
    public int getCount() {
        return iDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = View.inflate(context, R.layout.record_list_item, null);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        holder.sign = (ImageView) view.findViewById(R.id.iv_sign);
        holder.name = (TextView) view.findViewById(R.id.name);
        holder.count = (TextView) view.findViewById(R.id.count);
        holder.unit = (TextView) view.findViewById(R.id.tv_unit);
        holder.date = (TextView) view.findViewById(R.id.date);
        holder.confirmation_number = (TextView) view.findViewById(R.id.tv_confirmation_number);
        holder.ib_status_pedding = (ImageView) view.findViewById(R.id.ib_status_pedding);
        holder.ib_status_success = (ImageView) view.findViewById(R.id.ib_status_success);
        holder.rl_status = (RelativeLayout) view.findViewById(R.id.rl_status);
        holder.ib_kuang = (RelativeLayout) view.findViewById(R.id.ib_kuang);
        holder.mRl_all = (RelativeLayout) view.findViewById(R.id.rl_all);

        Glide.with(context).load(iDatas.get(i).getCoin_icon()).into(holder.sign);
        holder.name.setText(iDatas.get(i).getTx_hash().substring(0,8)+"..."+iDatas.get(i).getTx_hash().substring(iDatas.get(i).getTx_hash().length()-8,iDatas.get(i).getTx_hash().length()));
        holder.count.setText(iDatas.get(i).getValue_dec());
        holder.unit.setText("("+iDatas.get(i).getCoin_unit()+")");
        holder.date.setText(iDatas.get(i).getCreated_at().substring(0,10));
        holder.status = iDatas.get(i).getTx_status();
        int c = iDatas.get(i).getConfirmation_number();
        holder.confirmation_number.setText(String.valueOf(c));
        holder.mRl_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RecordListAdapter.this.click.Click(iDatas.get(i));
            }
        });

        if ("pending".equals(holder.status)){
            holder.ib_status_pedding.setVisibility(VISIBLE);
            holder.ib_status_success.setVisibility(View.GONE);
        }else if("sucess".equals(holder.status)){
            holder.ib_status_pedding.setVisibility(View.GONE);
            holder.ib_status_success.setVisibility(View.VISIBLE);
        }

        holder.rl_status.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (value == 1) {
                    holder.ib_kuang.setVisibility(View.VISIBLE);
                    holder.confirmation_number.setVisibility(View.VISIBLE);
                    value = 0;
                } else {
                    holder.ib_kuang.setVisibility(View.GONE);
                    holder.confirmation_number.setVisibility(View.GONE);
                    value = 1;
                }

            }
        });

        return view;
    }

    public interface OnClick {
        void Click(RecordBean.DataBean dataBean);

    }

    public void getClickPst(OnClick click) {
        this.click = click;
    }

    public class ViewHolder {
        public ImageView sign;//标志
        public TextView unit;//单位
        public TextView name;//地址
        public String status;//状态
        public ImageButton ib_status;//状态图
        public TextView date;//日期
        public TextView count;//转账金额
        public TextView confirmation_number;//同意数
        public ImageView ib_status_pedding;
        public ImageView ib_status_success;
        public RelativeLayout rl_status,ib_kuang,mRl_all;
    }
}
